package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yf1706.bean.EmpSkillCertificate;
import com.yf1706.bean.EmpWorkHistory;
import com.yf1706.bean.SkillLevelSetting;
import com.yf1706.bean.SkillSetting;
import com.yf1706.service.EmpService;
import com.yf1706.service.SettingService;
import com.yf1706.tools.UserTools;

@Controller
@RequestMapping("empSkillCertificate")
public class EmpSkillCertificateController {
	@Autowired
	private UserTools ut;
	@Autowired
	private EmpService es;
	@Autowired
	private SettingService ss;

	// 根据员工编号新增工作履历记录
	@RequestMapping("addEmpSkillCertificate")
	public void addEmpWorkHistory(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			out = response.getWriter();
			System.out.println("进入addEmpSkillCertificate");

			int empId = Integer.valueOf(request.getParameter("empId"));
			String certificateName = request.getParameter("certificateName");
			byte skillId = Byte.parseByte(request.getParameter("skillId"));
			byte skillLevelId = Byte.parseByte(request.getParameter("skillLevelId"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			Date endDate = sdf.parse(request.getParameter("endDate"));
			Date getDate = sdf.parse(request.getParameter("getDate"));
			String remark = request.getParameter("remark");
			SkillLevelSetting skillLevelSetting=ss.findSkillLevelBySkilLevellId(skillLevelId);
			SkillSetting skillSetting=ss.findSkillBySkillId(skillId);
			EmpSkillCertificate esc=new EmpSkillCertificate(beginDate, certificateName, endDate, getDate, remark, skillSetting, skillLevelSetting);
			es.addEmpSkillCertificateByEmpId(empId, esc);
			System.out.println(esc);
			map.put("success", true);
			map.put("msg", "添加成功");
			String msg = JSON.toJSONString(map);
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				map.put("success", false);
				map.put("msg", "日期格式不对");
				String msg = JSON.toJSONString(map);
				response.setCharacterEncoding("utf-8");
				response.getWriter().print(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// 根据编号删除工作履历记录
	@RequestMapping("deleteEmpSkillCertificateById")
	public void deleteEmpSkillCertificateById(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			out = response.getWriter();
			System.out.println("进入deleteEmpSkillCertificateById");
			System.out.println("id_arr" + request.getParameter("id_arr"));
			JSONArray arr = JSONArray.parseArray(request.getParameter("id_arr"));
			for (Object obj : arr) {
				int id = (int) obj;
				es.deleteEmpSkillCertificateByEmpId(id);
			}
			map.put("success", true);
			map.put("msg", "删除成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 修改对应编号的家庭信息记录
	@RequestMapping("updateEmpSkillCertificateById")
	public void updateEmpWorkHistoryById(HttpServletRequest request, HttpServletResponse response) {
		Map map = new HashMap<>();
		PrintWriter out;
		try {
			out = response.getWriter();

			System.out.println("进入updateEmpWorkHistoryById");

			int empId = Integer.valueOf(request.getParameter("empId"));
			int id = Integer.valueOf(request.getParameter("id"));
			String certificateName = request.getParameter("certificateName");
			byte skillId = Byte.parseByte(request.getParameter("skillId"));
			byte skillLevelId = Byte.parseByte(request.getParameter("skillLevelId"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			Date endDate = sdf.parse(request.getParameter("endDate"));
			Date getDate = sdf.parse(request.getParameter("getDate"));
			String remark = request.getParameter("remark");
			SkillLevelSetting skillLevelSetting=ss.findSkillLevelBySkilLevellId(skillLevelId);
			SkillSetting skillSetting=ss.findSkillBySkillId(skillId);
			
			EmpSkillCertificate esc=new EmpSkillCertificate(beginDate, certificateName, endDate, getDate, remark, skillSetting, skillLevelSetting);
			esc.setId(id);
			es.updateEmpSkillCertificateByEmpId(empId, esc);

			map.put("success", true);
			map.put("msg", "编辑成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				map.put("success", false);
				map.put("msg", "日期格式不对");
				response.getWriter().print(map);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
